<template>
  <el-card shadow="never">
    <div slot="header" ref="topHeader" class="sysUserSearchHeader">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item style="margin-bottom: 5px;" label="时间:">
          <el-date-picker
              v-model="listQuery.created_at"
              type="daterange"
              range-separator="至"
              start-placeholder=""
              end-placeholder=""
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" label="手机:">
          <el-input style="width: 130px" v-model="listQuery.phone" placeholder="手机" clearable/>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" label="搜索词:">
          <el-input style="width: 100px" v-model="listQuery.search" placeholder="搜索词" clearable/>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" label="来源类型:">
          <el-select
              v-model="listQuery.source_type" clearable placeholder="请选择">
            <el-option
                v-for="item in search_source_type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" label="来源:">
          <el-select
              v-model="listQuery.source"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请选择">
            <el-option
                v-for="item in search_source"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" label="领取人:">
          <el-select
              v-model="listQuery.u_id"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请选择">
            <el-option
                v-for="item in search_u_id"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" label="客户状态:">
          <el-select
              v-model="listQuery.state"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请选择">
            <el-option
                v-for="item in search_state"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" >
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item style="margin-bottom: 5px;" v-if="listQuery.page_type === 'info'">
          <el-button type="primary" icon="el-icon-circle-plus" @click="addEdit(null)">增加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <!--table data-->
      <el-table ref="tableCot" v-loading="loading" element-loading-text="" :data="tableData" border style="width: 100%" :max-height="customTableHeight">
        <el-table-column type="index" width="40"/>
        <el-table-column prop="brand" label="商标" min-width="50" />
        <el-table-column prop="source_type_name" label="来源类型" min-width="50" />
        <el-table-column prop="source_name" label="来源" min-width="30" />
        <el-table-column prop="search" label="搜索词" min-width="100" />
        <el-table-column label="ip" min-width="80">
          <template slot-scope="scope">
            <div>ip:{{ scope.row.ip }}</div>
            <div>归属:{{ scope.row.address_ip }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="地址" min-width="130">
          <template slot-scope="scope">
            <div v-if="scope.row.path_f != ''">首次:{{ scope.row.path_f }}</div>
            <div>提交:{{ scope.row.path }}</div>
          </template>
        </el-table-column>
        <el-table-column label="时间" min-width="110" >
          <template slot-scope="scope">
            <div>添加:{{ scope.row.created_at }}</div>
            <div v-if="scope.row.type == 0 && scope.row.update_at > 0">领取:{{ scope.row.update_at }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="user_pid === 1" prop="u_nick" label="领取人" min-width="50" />
        <el-table-column prop="state" label="状态" min-width="80" />
        <el-table-column prop="phone" label="电话" min-width="70" show-overflow-tooltip>
          <template slot-scope="scope">
            <i style="margin-right: 3px;" v-clipboard:copy='getCopyData(scope.row.phone)' v-clipboard:success='clipboardSuccess' title="复制电话" class="el-icon-copy-document"/>
            <span>{{ scope.row.phone }}</span>
            <div>{{ scope.row.address_phone }}</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80">
          <template slot-scope="scope">
            <el-tag style="cursor:pointer;margin-right: 4px;" size="mini" @click="toPage(scope.row.id)">
              <i class="el-icon-edit"/>详情
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!--分頁-->
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      <info-edit :dialog-visible="dialogVisible" :state="search_state" :source-type="search_source_type" @close="closeDialog"/>
    </div>
  </el-card>
</template>
<script>
import Pagination from '@/components/Pagination'
import infoEdit from '@/views/info/edit'
import { getList, getInfoConfig }  from '@/api/info'
import permission from '@/directive/permission/index.js'
import clipboard from '@/directive/clipboard/index.js'

export default {
  name: 'tenant',
  components: {Pagination, infoEdit},
  directives: {permission, clipboard},
  data() {
    return {
      loading: false,
      addEditVisible: false,
      operationType: 1,
      customTableHeight: 200,
      formInline: {
        user: '',
        region: ''
      },
      total: 0,
      listQuery: {
        page: 1,
        pageSize: 10,
        name: '',
        status: '',
        page_type: this.$route.name == 'Info' ? 'info' : 'all'
      },
      tableData: [],
      addEditData: {},
      search_state: [],
      search_source: [],
      search_source_type: [],
      search_u_id: [],
      user_pid: this.$store.getters.pid,
      dialogVisible: false,
    }
  },
  computed: {
    theme() {
      return this.$store.state.settings.theme
    }
  },
  mounted() {
    getInfoConfig().then(res => {
      this.search_source = res.data.source
      this.search_source_type = res.data.source_type
      this.search_u_id = res.data.u_id
      this.search_state = res.data.state
    })
    this.getList()
    let that = this
    window.onresize = () => {
      //37是box的header的padding，70=(60+10)是顶部的高度
      let tempHeaderHeight = this.$refs.topHeader.offsetHeight + 37 + 95
      let tempHeight = document.body.clientHeight - (106 + tempHeaderHeight)
      if (tempHeight < 300) {
        tempHeight = 300
      }
      that.customTableHeight = tempHeight
    }
    setTimeout(function () {
      const resizeEvent = new Event('resize')
      window.dispatchEvent(resizeEvent)
    }, 100)
  },
  destroyed() {
    window.onresize = null
  },
  methods: {
    search() {
      this.getList()
    },
    getList() {
      this.loading = true
      if (this.$route.name == 'Info') {
        this.listQuery.page_type = 'info'
      }
      getList(this.listQuery.page, this.listQuery).then(res => {
        this.tableData = res.data.list || []
        this.total = res.data.total || 0
        this.loading = false
        this.listQuery.page = res.data.page || 1
      }).catch(() => this.loading = false)
    },
    getCopyData(content) {
      return content
    },
    clipboardSuccess() {
      this.$message.success('复制成功')
    },
    toPage(id) {
      this.$router.push("/info_detail/" + id)
    },
    addEdit() {
      this.dialogVisible = true
    },
    closeDialog() {
      if (this.dialogVisible) {
        this.getList()
      }
      this.dialogVisible = false
    }
  }
}
</script>
<style scoped lang="scss">

</style>
